package com.House_to_rent_System.view;

import com.House_to_rent_System.domain.House;
import com.House_to_rent_System.service.HouseService;
import com.House_to_rent_System.utils.Utility;

import java.util.Scanner;

/**
 * 1.显示界面
 * 2.接收用户的输入
 * 3.调用service完成对房屋信息的各种操作
 */
public class HouseView {
    private boolean loop = true;//控制显示菜单
    private char key = ' ';//接收用户输入选择
    private HouseService houseService = new HouseService(1);//创建HouseService对象

    public void mainMenu() {
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.println("----------房屋出租系统------------");
            System.out.println("\t\t\t1.新 增 房 源");
            System.out.println("\t\t\t2.查 找 房 屋");
            System.out.println("\t\t\t3.删 除 房 屋");
            System.out.println("\t\t\t4.修 改 房 屋 信 息");
            System.out.println("\t\t\t5.房 屋 列 表");
            System.out.println("\t\t\t6.退 出 系 统");
            System.out.println("请选择操作（1-6）：");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    select();
                    break;
                case '3':
                    deleteHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouses();
                    break;
                case '6':
                    exit();
                    break;
                default:
                    System.out.println("输入错误，输入范围为1-6！");
            }

        } while (loop);
    }

    public void listHouses() {
        System.out.println("-----------房屋列表-------------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] list = houseService.list();
        for (int i = 0 ; i < list.length ; i++) {
            if (list[i] == null) {
                break;
            }
            System.out.println(list[i]);
        }
    }

    public void addHouse() {
        System.out.println("-----------添加房屋-------------");
        System.out.print("姓名:");
        String name = Utility.readString(8);
        System.out.print("电话:");
        String phone = Utility.readString(11);
        System.out.print("地址:");
        String address = Utility.readString(20);
        System.out.print("月租:");
        int rent = Utility.readInt(2000);
        System.out.println("状态(已出租/未出租)");
        String state = Utility.readString(4);
        House house = new House(0, name, phone, address, rent, state);
        if (houseService.add(house)) {
            System.out.println("-----------添加成功-------------");
        } else {
            System.out.println("-----------添加失败-------------");
        }
    }

    public void deleteHouse() {
        System.out.println("-----------删除房屋-------------");
        System.out.print("请输入要删除房屋的编号(输入-1为放弃删除)：");
        int houseId = Utility.readInt();
        if (houseId == -1) {
            System.out.println("你放弃了删除房间");
            return;
        }
        char choice = Utility.readConfirmSelection();
        if (choice == 'Y') {
            if (houseService.deleteHouse(houseId)) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败...");
            }
        } else {
            System.out.println("放弃删除");
        }
    }

    public void exit() {
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            loop = false;
        }
    }

    public void select() {
        System.out.println("-----------查找房屋-------------");
        System.out.print("请输入查找的房屋编号");
        int HouseId = Utility.readInt();
        House house = houseService.selectHouse(HouseId);
        if (house == null) {
            System.out.println("查找的房屋信息不存在");
        } else {
            System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
            System.out.println(house);
        }
    }

    public void updateHouse() {
        System.out.println("-----------修改房屋-------------");
        System.out.println("请输入要修改房屋的编号(输入-1退出修改):");
        int updateId = Utility.readInt();
        if (updateId == -1) {
            System.out.println("-------------放弃修改---------------");
            return;
        }
        House house = houseService.selectHouse(updateId);
        if (house == null){
            System.out.println("要修改的房屋编号不存在..");
            return;
        }
        System.out.println("姓名("+house.getName()+"):");
        String name = Utility.readString(8,"");
        if (!"".equals(name)){
            house.setName(name);
        }
        System.out.println("电话("+house.getPhone()+"):");
        String phone = Utility.readString(11,"");
        if (!"".equals(phone)){
            house.setPhone(phone);
        }
        System.out.println("地址("+house.getAddress()+"):");
        String address = Utility.readString(20,"");
        if (!"".equals(address)){
            house.setAddress(address);
        }
        System.out.println("租金("+house.getRent()+"):");
        int rent = Utility.readInt(2000);
        if (rent != 2000){
            house.setRent(rent);
        }
        System.out.println("状态("+house.getState()+"):");
        String state = Utility.readString(4,"");
        if (!"".equals(state)){
            house.setState(state);
        }
        System.out.println("--------修改成功--------");
        System.out.println(houseService.selectHouse(updateId));
    }
}
